<?php
class Dmf_Form_Element_Select extends Dmf_Form_Element
{
    private $_options = array();
    
    public function addOption( $name, $value, $attributes = array() )
    {
        //Remove selected attribute because when validated the selected element becomes selected
        if ( strtolower( $_SERVER['REQUEST_METHOD'] ) == 'post' )
        {
            unset( $attributes['selected'] );
        }
        
        $option = new Dmf_Form_Element_Option;
        
        $option->form = $this->form;
        $option->setAttrs( $attributes );
        $option->setHtml( $value );
        $option->setAttr( 'value', $name );
        
        $this->_options[] = $option;
        
        return $this;
    }
    
    public function getContent()
    {
        $options = '';
        
        foreach( $this->_options as $option )
        {
            if ( $this->getAttr('value') === $option->getAttr('value') )
            {
                $option->setAttr( 'selected' );
            }
            
            $options .= PHP_EOL . '                ' . $option->render();
        }
        
        return '<select%attributes%>' . $options . '            </select>';
    }
}